﻿using Microsoft.AspNetCore.Mvc;
using SmartParking.Dto.Req;
using SmartParking.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json.Nodes;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace SmartParking.Controller
{
    public class MultistepPriceController : ApplicationService
    {
        private readonly IRepository<MultistepPrice> _multistepPriceDb;

        public MultistepPriceController(IRepository<MultistepPrice> multistepPriceDb)
        {
            _multistepPriceDb = multistepPriceDb;
        }

        [HttpPost]
        public virtual async Task<IActionResult> Insert([FromBody] MultistepPriceReq req)
        {
            var query = await _multistepPriceDb.GetQueryableAsync();
            query = query.Where(p => p.PlanId == req.PlanId && p.LowerLimit < p.LowerLimit && p.UpperLimit > p.LowerLimit);
            var oldPrice = query.First();

            var oldUpperLimit = oldPrice.UpperLimit;
            oldPrice.UpperLimit = req.LowerLimit;
            oldPrice = await _multistepPriceDb.UpdateAsync(oldPrice);

            var newPrice = ObjectMapper.Map<MultistepPriceReq, MultistepPrice>(req);
            newPrice.UpperLimit = oldUpperLimit;

            newPrice = await _multistepPriceDb.InsertAsync(newPrice);
            return new JsonResult(newPrice);
        }
        [HttpPost]
        public virtual async Task<IActionResult> CheckInsertable([FromBody] MultistepPriceReq req)
        {
            var query = await _multistepPriceDb.GetQueryableAsync();
            query = query.Where(p => p.PlanId == req.PlanId && p.LowerLimit < p.LowerLimit && p.UpperLimit > p.LowerLimit);
            var result = new JsonObject
            {
                ["editable"] = query.Any()
            };
            return new JsonResult(result);
        }

        [HttpPost]
        public virtual async Task<IActionResult> CheckUpdatable([FromBody] MultistepPriceReq req)
        {
            var query = await _multistepPriceDb.GetQueryableAsync();
            var self = (from p in query where p.Id == req.Id select p).First();
            var previous = (from p in query where p.LowerLimit < self.LowerLimit orderby p.LowerLimit descending select p).First();
            var result = new JsonObject
            {
                ["updatable"] = previous.LowerLimit < req.LowerLimit
            };
            return new JsonResult(result);
        }
        [HttpPut]
        public virtual async Task<IActionResult> Update([FromBody] MultistepPriceReq req)
        {
            var query = await _multistepPriceDb.GetQueryableAsync();
            var self = (from p in query where p.Id == req.Id select p).First();
            var previous = (from p in query where p.LowerLimit < self.LowerLimit orderby p.LowerLimit descending select p).First();
            self.LowerLimit = req.LowerLimit;
            previous.UpperLimit = req.LowerLimit;
            self = await _multistepPriceDb.UpdateAsync(self);
            await _multistepPriceDb.UpdateAsync(previous);
            return new JsonResult(self);
        }
    }
}
